package fr.ups.jim.isi.tp4;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.Stroke;

public abstract class CanvasItem {
	
	protected PersistentCanvas canvas;
	protected Color outlineColor, fillColor;
	protected boolean selected;
	protected Shape shape;
	
	CanvasItem (PersistentCanvas canvas, Color oColor, Color fColor) {
		outlineColor = oColor;
		fillColor = fColor;
		this.canvas = canvas;
		selected = false;
	}
	
	public void setOutlineColor(Color c) {
		outlineColor = c;
		canvas.repaint();
	}
	
	public void setFillColor(Color c) {
		fillColor = c;
		canvas.repaint();
	}
	
	public void selected() {
		selected = true;
		canvas.repaint();
	}
	
	public void deselected() {
		selected = false;
		canvas.repaint();
	}
	
	public boolean contains(Point p) {
		if (shape.contains(p))
			return true;
		else 
			return false;
	}
	
	public void paint(Graphics2D g) {
		// fill the shape 
		g.setColor(fillColor);
		g.fill(shape);
		
		// draw the shape
		g.setColor(outlineColor);
		Stroke originStroke = null;
		if(selected) {
			originStroke = g.getStroke();
			g.setStroke(new BasicStroke(3));
		}
		g.draw(shape);	
		if (originStroke != null)
			g.setStroke(originStroke);
	}
	
	/**
	 * This method is used for drawing a complete shape after the constructor
	 * @param p		the second point for completely determine the shape, for
	 * 				example, for Rectangle, it's another endpoint of its 
	 * 				diagonal
	 */
	public abstract void update(Point p);
	
	/**
	 * This method creates another CanvasItem with same size while starts from 
	 * another coordinate, whose X Y coordinate are always 10 pixels larger
	 * than original X Y coordinate
	 * @return		created CanvasItem
	 */
	public abstract CanvasItem duplicate();
	
	/**
	 * This method should be able to move the selected CanvasItem when the 
	 * mouse dragged it
	 * @param disX	the distance dragged on the X axis
	 * @param disY	the distance dragged on the Y axis
	 */
	public abstract void move(int disX, int disY);
}
